package lg.minecraft.plugin.worldprotect.listener;

import lg.minecraft.plugin.worldprotect.LGWorldProtect;
import lg.minecraft.plugin.worldprotect.WorldSettingType;
import lg.minecraft.plugin.worldprotect.util.WorldSettings;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.*;

/**
 * @author langle__
 * @version 1.0
 */
public class BlockEvent implements Listener {

    @EventHandler(priority = EventPriority.LOWEST)
    public void onBlockPlace(BlockPlaceEvent event) {
        if (event.isCancelled()) return;
        Player player = event.getPlayer();
        World world = player.getWorld();

        if (player.isOp()) return;

        WorldSettings worldSettings = LGWorldProtect.getWorldSettings(world);

        if (worldSettings.isEnabled(WorldSettingType.PLACE)) {
            if (LGWorldProtect.getMainConfig().isEnableTip()) player.sendMessage(LGWorldProtect.getLanguage().getWorldSettingTip(WorldSettingType.PLACE));
            event.setCancelled(true);
        }

    }

    @EventHandler(priority = EventPriority.LOWEST)
    public void onBlockBreak(BlockBreakEvent event) {
        if (event.isCancelled()) return;
        Player player = event.getPlayer();
        World world = player.getWorld();

        if (player.isOp()) return;

        WorldSettings worldSettings = LGWorldProtect.getWorldSettings(world);

        if (worldSettings.isEnabled(WorldSettingType.DESTRUCTION)) {
            if (LGWorldProtect.getMainConfig().isEnableTip()) player.sendMessage(LGWorldProtect.getLanguage().getWorldSettingTip(WorldSettingType.DESTRUCTION));
            event.setCancelled(true);
        }

    }

    @EventHandler(priority = EventPriority.LOWEST)
    public void onLeavesDecay(LeavesDecayEvent event) {
        Block block = event.getBlock();
        World world = block.getWorld();

        WorldSettings worldSettings = LGWorldProtect.getWorldSettings(world);
        if (worldSettings.isEnabled(WorldSettingType.DECAY)) {
            event.setCancelled(true);
        }
    }

    @EventHandler(priority = EventPriority.LOWEST)
    public void onBlockFromTo(BlockFromToEvent event) {
        if (event.isCancelled()) return;
        Block block = event.getBlock();
        World world = block.getWorld();
        Material type = block.getType();

        WorldSettings worldSettings = LGWorldProtect.getWorldSettings(world);
        if (worldSettings.isEnabled(WorldSettingType.FLOW) && type == Material.WATER) {
            event.setCancelled(true);
        }
        if (worldSettings.isEnabled(WorldSettingType.LAVA_FLOW) && type == Material.LAVA) {
            event.setCancelled(true);
        }

        if (worldSettings.isEnabled(WorldSettingType.DRAGON_EGG_TELEPORT) && type == Material.DRAGON_EGG) {
            event.setCancelled(true);
        }

    }

    @EventHandler(priority = EventPriority.LOWEST)
    public void onBlockFade(BlockFadeEvent event) {
        if (event.isCancelled()) return;
        Block block = event.getBlock();
        Material type = block.getType();
        World world = block.getWorld();

        WorldSettings worldSettings = LGWorldProtect.getWorldSettings(world);
        if (worldSettings.isEnabled(WorldSettingType.ICE_FORM) && (type == Material.ICE || type == Material.FROSTED_ICE)) {
            event.setCancelled(true);
        }
        if (worldSettings.isEnabled(WorldSettingType.SNOW_FORM) && (type == Material.ICE || type == Material.FROSTED_ICE)) {
            event.setCancelled(true);
        }
    }

    @EventHandler(priority = EventPriority.LOWEST)
    public void onBlockForm(BlockFormEvent event) {
        if (event.isCancelled()) return;
        Block block = event.getBlock();
        Material type = block.getType();
        World world = block.getWorld();

        WorldSettings worldSettings = LGWorldProtect.getWorldSettings(world);
        if (worldSettings.isEnabled(WorldSettingType.ICE_MELT) && type == Material.WATER) {
            event.setCancelled(true);
        }
        if (worldSettings.isEnabled(WorldSettingType.SNOW_MELT) && type == Material.AIR) {
            event.setCancelled(true);
        }
    }

    @EventHandler(priority = EventPriority.LOWEST)
    public void onBlockSpread(BlockSpreadEvent event) {
        if (event.isCancelled()) return;
        Block block = event.getBlock();
        Material type = block.getType();
        World world = block.getWorld();

        WorldSettings worldSettings = LGWorldProtect.getWorldSettings(world);
        if (worldSettings.isEnabled(WorldSettingType.FIRE_SPREAD) && type == Material.FIRE) {
            event.setCancelled(true);
        }

    }

    @EventHandler(priority = EventPriority.LOWEST)
    public void onBlockIgnite(BlockIgniteEvent event) {
        if (event.isCancelled()) return;
        Block block = event.getBlock();
        World world = block.getWorld();

        WorldSettings worldSettings = LGWorldProtect.getWorldSettings(world);
        if (worldSettings.isEnabled(WorldSettingType.IGNITE)) {
            event.setCancelled(true);
        }
    }

    @EventHandler(priority = EventPriority.LOWEST)
    public void onBlockDispense(BlockDispenseEvent event) {
        if (event.isCancelled()) return;
        Block block = event.getBlock();
        World world = block.getWorld();
    }

}
